// pages/activityList/activityList.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 轮播数据
        wheelData: [],
        showSwiperIndex: 0,
        page: 1,
        hasMore: true,
        data: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.onPullDownRefresh();
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        this.loadWheelData();
        this.loadData(1).finally(() => {
            wx.stopPullDownRefresh();
        });
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        if (!this.data.hasMore) return;

        const page = this.data.page + 1;
        this.loadData(page);
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {
        const image = "";
        return {
            imageUrl: image,
            path: this.route
        }
    },

    // 广告位改变
    onSwiperChange: function(e) {
        this.setData({
            showSwiperIndex: e.detail.current
        });
    },

    // 单击广告位
    onWheelToTap: function(e) {
        const dataset = e.currentTarget.dataset,
            index = dataset.index;
        const item = this.data.wheelData[index];
        console.log(item)
        if (item.type.value == 10) {
            wx.navigateTo({
                url: '/pages/mall/goods-detail/goods-detail?id=' + item.goods_id,
            });
        } else if (item.type.value == 20) {
            wx.navigateTo({
                url: '/pages/course/detail/detail?id=' + item.goods_id,
            });
        } else if (item.type.value == 30) {
            wx.navigateTo({
                url: '/pages/activity/detail/detail?id=' + item.goods_id,
            });
        }
    },

    // 加载轮播
    loadWheelData: function() {
        return wx.http.get('api/index/wheel', {
            display_type: 30
        }).then((res) => {
            res = res.data.data.getList;
            this.setData({
                wheelData: res,
            });
            return res;
        }, (err) => {
            console.warn("api/index/wheel", err)
        });
    },

    // 加载数据
    loadData: function(page = 1) {
        return wx.http.get('api/activity/lists', {
            page: page,
        }).then((res) => {
            res = res.data.data.categoryList;
            const hasMore = res.data.length >= res.pre_page;
            const data = page > 1 ? this.data.data.concat(res.data) : res.data;
            this.setData({
                page: page,
                hasMore: hasMore,
                data: data,
            });
            return res.data;
        }, (err) => {
            console.warn("api/activity/lists", err)
        });
    },
})